import {HeroData} from "../data/HeroData";
import {ToolUtils} from "../util/ToolUtils";
import {DispatcherUtils} from "../util/DispatcherUtils";
import {Event} from "../Event";
import {network} from "../common/network";
import {InfoModule} from "../logic/InfoModule";
import {HINT_TYPE, HintUI} from "../custom/HintUI";

const {ccclass, property} = cc._decorator;
@ccclass
export default class LobbyScene extends cc.Component {
    @property(cc.Label)
    giftLabel = null;
    @property(cc.Label)
    goldLabel = null;
    @property(cc.Label)
    diamondLabel = null;
    @property(cc.Label)
    nameLabel = null;
    @property(cc.Sprite)
    headSprite = null;
    onLoad(){
        this.initViewEvent();
        if(!network.isConnected()){
            if(HeroData.uid == 0){
                cc.find("登录",this.node).active = true;
            }
            else{
                //TODO 断线重连
                /*cc.find("登录",this.node).active = false;
                LoginModule.reconnect();*/
            }
        }
        this.updateName();
        this.updateGold();
    }

    onClickRoom(e,data){
        InfoModule.enterRoom(data);
    }

    onClickMoreBtn(e,data){
        let node = cc.find("Canvas/下部节点/更多")
        node.active = !node.active
    }

    onClickBtn(e,data){
        let datas = data.split(",");
        let node = cc.find("Canvas/"+datas[0]);
        if(node){
            /*let tag = parseInt(datas[1]);
            if(!isNaN(tag)){
                node.tag = tag;
            }*/
            node.active = true;
        }
    }

    onClickQuickStart(){
        //TODO
    }

    private updateName(){
        if(this.nameLabel == null) return;
        ToolUtils.nameToShort(HeroData.nickname,this.nameLabel,120);
    }

    private updateGold(){
        if(this.goldLabel == null) return;
        ToolUtils.numToShort(HeroData.gold,this.goldLabel,83);
    }

    private updateInTableID(){
        if(this.node == null) return;
        if(HeroData.tableId != 0){
            console.log("updateInRoomID")
            HintUI.show("您还有正在进行的游戏，是否进入？",HINT_TYPE.TWO,function (flag) {
                if(flag){
                    InfoModule.relinkRoom();
                }
            });
        }
    }

    private onKicked(){
        let loginNode = cc.find("登录",this.node);
        if(cc.find("登录",this.node)){
            loginNode.active=true;
        }else{
            cc.director.loadScene("LobbyScene")
        }
    }

    private initViewEvent(){
        let self = this;
        DispatcherUtils.removeEventListenerByEvent(Event.HERO_NAME_UPDATE);
        DispatcherUtils.removeEventListenerByEvent(Event.HERO_GOLD_UPDATE);
        DispatcherUtils.removeEventListenerByEvent(Event.HERO_TABLEID_UPDATE);
        DispatcherUtils.removeEventListenerByEvent(Event.SERVER_KICKED);
        DispatcherUtils.addEventListener(Event.HERO_NAME_UPDATE,function () {
            console.log("update name",HeroData.nickname);
            self.updateName();
        });
        DispatcherUtils.addEventListener(Event.HERO_GOLD_UPDATE,function () {
            console.log("update gold",HeroData.gold);
            self.updateGold();
        });
        DispatcherUtils.addEventListener(Event.HERO_TABLEID_UPDATE,function () {
            console.log("update tableid",HeroData.tableId);
            self.updateInTableID();
        })
        DispatcherUtils.addEventListener(Event.SERVER_KICKED,function () {
            console.log("update server kicked");
            self.onKicked();
        })
    }
}